package com.tencent.timsdk.emotion;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.jcp.baselibrary.utils.SPUtil;
import com.jcp.baselibrary.utils.UIUtils;
import com.tencent.timsdk.R;

public class EmotionAdapter extends BaseAdapter {

    private Context mContext;
    private int mStartIndex;
    private int mEmotionLayoutWidth;
    private int mEmotionLayoutHeight;
    private final int mPerWidth;
    private final int mPerHeight;

    private AbsListView.LayoutParams params;

    public EmotionAdapter(Context context, int startIndex) {
        mContext = context;
        mStartIndex = startIndex;
        mEmotionLayoutWidth = (int) UIUtils.getScreenWidth();
        //减去底部的tab高度、小圆点的高度才是viewpager的高度，再减少30dp是让表情整体的顶部和底部有个外间距  30 + 35
        mEmotionLayoutHeight = (int) (SPUtil.getInt(EmotionChatInput.SHARE_PREFERENCE_SOFT_INPUT_HEIGHT, UIUtils.dp2px(270)) - UIUtils.dp2px(65));

        mPerWidth = mEmotionLayoutWidth/ EmotionLayout.EMOJI_COLUMNS;
        mPerHeight = mEmotionLayoutHeight / EmotionLayout.EMOJI_ROWS;
        params = new AbsListView.LayoutParams(mPerWidth, mPerHeight);
    }

    @Override
    public int getCount() {
        int count = EmotionManager.getDisplayCount() - mStartIndex + 1;
        count = Math.min(count, EmotionLayout.EMOJI_PER_PAGE + 1);
        return count;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return mStartIndex + position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView emojiThumb = new ImageView(mContext);
        emojiThumb.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        int count = EmotionManager.getDisplayCount();
        int index = mStartIndex + position;
        if (position == EmotionLayout.EMOJI_PER_PAGE || index == count) {
            emojiThumb.setImageResource(R.drawable.icon_emoji_del);
        } else if (index < count) {
            String text = EmotionManager.getDisplayText(index);
            emojiThumb.setImageBitmap(EmotionManager.getBitmap(mContext, text));
        }
        emojiThumb.setLayoutParams(params);
        return emojiThumb;
    }
}
